import {baseEffects, baseReducers, baseState} from "../../../../models/baseModel";
import * as request from "../../../../services/CommonHttpService";
import {orderGetName} from "../../../../models/OrderStatus";
import moment from "moment";
import {payTypeGetName} from "../../../../models/PayType";
import PageHelper from "utils/pageHelper";
import {tradeStateGetName} from "../../../../models/TradeState";

export default {
  namespace: 'orderDetail',

  state: {
    ...baseState,
    userInfo:{},
    orderInfo: {
      payLog: {},
      orderLogistics: {}
    },
    waitSend: false,  // 等待发货
    goodItemsPage: {},
  },

  reducers: {
    ...baseReducers
  },

  effects: {

    ...baseEffects,

    * getOrderInfo({payload}, {call, put}) {

      const {orderId} = payload;

      const {errorMsg, process, baseUrl, notice, server} = payload;

      try {
        let result = yield call(request.commonGet, `order/${orderId}`, {}, {}, server);

        const {payLog, orderLogistics, orderStatus, returnStatus, orderDetailGoodsVos} = result;

        const waitSend = orderStatus == "PAY_OK";
        const waitRefund = orderStatus == "RETURN";
        const goodItemsPage = PageHelper.wrapPage(orderDetailGoodsVos.map(v => {
          return {...v, price: v.price / 100};
        }));

        const orderInfo = {
          ...result,
          payment: parseInt(result.payment) / 100 + "元",
          postFee: parseInt(result.postFee) / 100 + "元",
          paymentType: result.paymentType == 1 ? "在线支付" : "货到付款",
          orderStatus: orderGetName(orderStatus, returnStatus),

          createTime: !result.createTime ? "" : moment(result.createTime).format('YYYY-MM-DD HH:mm:ss'),
          paymentTime: !result.paymentTime ? "" : moment(result.paymentTime).format('YYYY-MM-DD HH:mm:ss'),
          consignTime: !result.consignTime ? "" : moment(result.consignTime).format('YYYY-MM-DD HH:mm:ss'),
          endTime: !result.endTime ? "" : moment(result.endTime).format('YYYY-MM-DD HH:mm:ss'),
          closeTime: !result.closeTime ? "" : moment(result.closeTime).format('YYYY-MM-DD HH:mm:ss'),
          expireTime: orderStatus == "NOT_PAY" ?
            (!result.expireTime ? "" : moment(result.expireTime).format('YYYY-MM-DD HH:mm:ss')) : "",

          orderLogistics: {
            ...orderLogistics,
          },

          payLog: {
            ...payLog,
            payTime: !payLog.payTime ? "" : moment(payLog.payTime).format('YYYY-MM-DD HH:mm:ss'),
            totalFee: parseInt(payLog.totalFee) / 100 + "元",
            payType: payTypeGetName(payLog.payType),
            tradeState: tradeStateGetName(payLog.tradeState),
          }
        };

        console.log("orderInfo", orderInfo);

        yield put({
          type: 'initState',
          payload: {orderInfo, waitSend, waitRefund, goodItemsPage}
        });

        yield put({
          type: 'getUserInfo',
          payload: {...payload, value:{userId: orderInfo.userId}}
        });

      } catch (error) {
        console.error("baseGetAll", error);
        yield put({type: 'loadAllError'});
        !!notice && notice("error", !!errorMsg ? errorMsg : error);
      }
    },

    * refund({payload}, {call, put, select}) {

      const {value, success, fail, errorMsg, notice, server} = payload;
      const {orderId, refundFee} = value;

      const http = request.commonPut;
      const url = `${getBaseUrl()}/${orderId}/operateReturn`;

      yield put({type: 'changing'});

      try {
        let sucess = yield call(http, url, {}, {refundFee: refundFee*100});
        yield put({type: 'changSucess'});
        success();
      } catch (error) {
        console.error("refund", error);
        yield put({type: 'changError'});
        !!notice && notice("error", !!errorMsg ? errorMsg : error);
        fail();
      }
    },

    * getUserInfo({payload}, {call, put, select}) {
      const {value, success, fail, errorMsg, notice, server} = payload;
      const {userId} = value;

      const http = request.commonGet;
      const url = `/user/${userId}`;

      try {
        let userInfo = yield call(http, url, {}, {});
        console.log("userInfo", userInfo);
        yield put({
          type: 'initState',
          payload: {userInfo}
        });
        !!success && success();
      } catch (error) {
        console.error("getUserInfo", error);
        yield put({type: 'changError'});
        !!notice && notice("error", !!errorMsg ? errorMsg : error);
        !!fail && fail();
      }
    }

  }

}

function getBaseUrl() {
  return "/order";
}

